<template>
    <view class="page-all">
        <Header title="选择学校" bgColor="#fff" :leftIconSize="20"></Header>
        <view class="search-content">
            <view class="left">
                <uv-search placeholder="学校名称" bgColor="#fff" v-model="queryForm.name" :showAction="false"  @change="searchFun()" @clear="clearFun()"></uv-search>
            </view>
            <view class="screen-icon" @click="showScreen">
                <image v-if="iconFlag === true" src="@/static/image/index/icon2.png" mode="widthFix"></image>
                <image v-else src="@/static/image/index/icon1.png" mode="widthFix"/>
            </view>
        </view>
        <view v-if="emptyFlag" class="empty-box">
            <uv-empty mode="data" icon="/static/image/no-data.png" width="200rpx" height="260rpx" text="&nbsp;"/>
        </view>
        <!--学校列表-->
        <view class="school-list" v-else>
            <uv-radio-group v-model="radiovalue">
                <view class="container">
                    <view class="item" v-for="(item,index) in dataList" :key="index">
                        <view class="radio">
                            <uv-radio :name="item.id" :disabled="item.finishFlag != '0' && checkFlag">
                                <template #default>
                                    <view class="name">{{ item.name }}</view>
                                </template>
                            </uv-radio>
                        </view>
                    </view>
                </view>

            </uv-radio-group>
        </view>
        <!--筛选弹层-->
        <uv-popup ref="popup" @change="change" :closeable="true">
            <view class="popup-title">筛选</view>
            <view class="popup-content">
                <view class="popup-title-row">
                    <text>办学性质</text>
                </view>
                <view class="popup-con-row">
                    <view class="item" :class="{actived:schoolPropertiesCode==item.value}" v-for="(item,index) in natureList" :key="index"
                          @click="schoolPropertiesCode=item.value">{{item.name}}</view>
                </view>
                <view class="popup-title-row">
                    <text>办学类型</text>
                </view>
                <view class="popup-con-row">
                    <view class="item" :class="{ actived: doSchoolTypeCode == item.value }" v-for="(item, index) in typeList"
                          :key="index" @click="doSchoolTypeCode=item.value">
                        {{ item.name }}
                    </view>
                </view>
                <view class="popup-title-row"  v-if="areaList.length>0">
                    <text>地区</text>
                </view>
                <view class="popup-con-row">
                    <view v-for="(item,index) in areaList" :key="index" class="item"
                          :class="{actived:item.code === area}" @click="areaFun(item)">
                        {{ item.name }}
                    </view>
                </view>
                <view class="bottom-box" @click.stop>
                    <button class="yellow-sure-button" style="width: 49%" @click.stop="resetForm()">重置</button>
                    <button class="sure-button" @click.stop="popupSubmit()">确认</button>
                </view>
            </view>
        </uv-popup>
        <view class="check-bottom-box" @click.stop>
            <button class="sure-button" @click.stop="submit()">确认</button>
        </view>

    </view>
</template>

<script setup>
import {getCurrentInstance, reactive, ref} from 'vue'

const {proxy} = getCurrentInstance();
import publicAPI from "@/api/law/public/public";
import supTaskAPI from "@/api/law/supTask/supTask";
import dictApi from "@/api/dict/dict";
import {onLoad} from "@dcloudio/uni-app";
import config from "@/common/config";
let imgMinioPath = ref(config.IMG_MINIO_PATH);
const userInfo = ref(uni.getStorageSync('userInfo'));
const radiovalue = ref('')
const schoolPropertiesCode = ref()  //选中的学校性质
let dataList = ref([])
let natureList = ref([])
let task = ref({});
let typeList = ref([]);
const emptyFlag = ref(false);
let iconFlag = ref(false);
const doSchoolTypeCode = ref()  //选中的办学类型
const queryForm = reactive({ // 查询条件
    name: '', // 机构名称（学校/配餐单位）
    schoolPropertiesCode: '', // 办学性质
    doSchoolTypeCode: '', // 办学类型
    countyId: '', // 地区
    params: {
        sourceIds: []
    }
});
let form = reactive({})
//点击重置按钮
function resetForm() {
    iconFlag.value = false;
    schoolPropertiesCode.value = '';
    doSchoolTypeCode.value = '';
    area.value = '';
    queryForm.name= '';
    getSchoolList();
    popup.value.close()
}
let changeIcon = () => {
    if (area.value || doSchoolTypeCode.value || schoolPropertiesCode.value) {
        iconFlag.value = true;
    } else {
        iconFlag.value = false;
    }
}
let schoolIds = ref();

onLoad(async (e) => {
    let data = JSON.parse(decodeURIComponent(e.data))
    task.value = data;
    schoolIds = data.schoolId.split(',')
    if (schoolIds) {
        getSchoolList();
    }
    uni.setStorageSync('entryType', data.entryType);
});

let searchFun = () => {
    getSchoolList();
};
/** 清空搜索 */
let clearFun = () => {
    getSchoolList();
}

// 查询学校数据
let getSchoolList = async () => {
    queryForm.params.sourceIds = schoolIds
    let res = await proxy.http.asyncPost(publicAPI.getOrgSchoolNoTenantId, queryForm);
    if (res.code === 0) {
        dataList.value = res.data;
        emptyFlag.value = dataList.value.length <= 0;
        getTaskOrgList()
    } else {
        await uni.showToast({title: res.msg, icon: 'none'});
    }
};
let checkFlag = ref(false);
// 查询学校是否检查
let getTaskOrgList = async () => {
    checkFlag.value = false;
    let res = await proxy.http.asyncPost(supTaskAPI.getTaskOrgList, {taskId:task.value.id});
    if (res.code === 0) {
        checkFlag.value = true;
        let list = res.data
        dataList.value.forEach(item => {
            const taskOrg = list.find(task => task.schoolId === item.id);
            if (taskOrg) {
                item.finishFlag = taskOrg.finishFlag;
            }
        });
    } else {
        await uni.showToast({title: res.msg, icon: 'none'});
    }
};


//获取学校性质
let getNatureList = async () => {
    let res = await proxy.http.asyncGet(dictApi.findDataListByType('school_properties'), {});
    if (res.code === 0) {
        natureList.value = [...res.data];
    }
}

//返回把选中学校数据传回
function submit() {
    let currentObj = dataList.value.find((item=>item.id == radiovalue.value))
    form.schoolName = currentObj.name
    form.schoolId = currentObj.id
    form.taskId = task.value.id
    form.taskTargetName = task.value.targetName
    form.lawType = '2'
    if (!form.schoolId) {
        uni.showToast({
            icon: 'error',
            title: '请先选择学校！'
        })
        return;
    }
    uni.navigateTo({
        url: `/packageLawEnforcement/pages/startCheck/index?data=${encodeURIComponent(JSON.stringify(form))}`
    })
}

let popup = ref()

//显示筛选弹层
function showScreen() {
    popup.value.open('bottom')
}

function popupSubmit() {
    queryForm.schoolPropertiesCode=schoolPropertiesCode.value
    queryForm.doSchoolTypeCode=doSchoolTypeCode.value
    if (areaType.value === '1') {
        queryForm.cityId = area.value;
    } else {
        queryForm.countyId = area.value;
    }
    changeIcon();
    popup.value.close()
    getSchoolList();
}
//获取码表-办学类型
let getTypeList = async () => {
    let res = await proxy.http.asyncGet(dictApi.findDataListByType('do_school_type'), {});
    if (res.code === 0) {
        typeList.value = [...res.data];
    }
}
/** 筛选-地区 */
const area = ref('');
const areaList = ref([]);
let areaType = ref('');
// 获取地区列表
let getAreaList = async () => {
    let query = {};
    if (userInfo.value.user.org.county) { // 区
        areaType.value = '3';
        return;
    } else if (userInfo.value.user.org.city) { // 市
        areaType.value = '2';
        query.parentCode = userInfo.value.user.org.city;
    } else { // 省
        areaType.value = '1';
        query.parentCode = userInfo.value.user.org.province;
    }
    let res = await proxy.http.asyncPost(publicAPI.getAreaListByParentCode(query.parentCode));
    if (res.code === 0) {
        areaList.value = reactive(res.data);
    }
};
// 筛选选中
const areaFun = (item) => {
    if (area.value === item.code) {
        area.value = ''
    } else {
        area.value = item.code;
    }
};
//页面加载
onMounted(async () => {
    // getSchoolList();
    getNatureList();
    getTypeList();
    getAreaList();
})


</script>
<style scoped>
	.bottom-box{
		padding: 0;
	}
  .name {
    width: 100% !important;
    line-height: 3.4375rem;
  }
</style>
